package com.yisu.auction.api.web.controller;


import com.yisu.auction.api.core.service.IMemberService;
import com.yisu.auction.api.core.service.IOfferPriceLogService;
import com.yisu.auction.api.domain.config.SystemConfig;
import com.yisu.auction.ewf.common.Constants;
import com.yisu.auction.ewf.entity.UserToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;
import java.text.SimpleDateFormat;

import static com.yisu.auction.ewf.common.enums.ResponseCode.SUCCESS;


/**
 * author: 赵杰
 * Business Description：基础控制器
 * 时间：2017/8/7
 */
public class BaseController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    protected final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Resource
    private SystemConfig systemConfig;
    @Resource
    private IMemberService iMemberService;
    @Resource
    private IOfferPriceLogService iOfferPriceLogService;

    public Date dbDate;

    public Date getDbDate() {
        dbDate = iOfferPriceLogService.getDbDate();
        if (dbDate == null)
            return new Date();
        return dbDate;
    }

    public boolean getAllowAuction() {
        if (systemConfig.isStopPurchase()) {
            Date date = new Date();
            int hour = date.getHours();
            if (hour >= 1 && hour < 6) {
                return false;
            }
            return true;
        }
        return true;
    }


    //用户token，参数传值
    public String userToken;

    //用户令牌信息
    public UserToken token;

    public UserToken getToken() {
        token = iMemberService.getUserTokenByToken(userToken);
        if (token == null)
            return new UserToken();
        return token;
    }


    @ModelAttribute
    public void setResponseHeader(HttpServletResponse response) {
        response.setHeader(Constants.REQUEST_RESULT_CODE, String.valueOf(SUCCESS.code));
    }


}
